<?php
$this->LoadHelper('TaskHelper');
$TaskHelper = new TaskHelper();

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('FtpUploadHelper');
$FtpUploadHelper = new FtpUploadHelper();

$this->LoadHelper('AttachHelper');
$AttachHelper = new AttachHelper($this->UserConfig, 'task');

$where = array(
    '`task_type` = ?' => 'ordersort',
    '`task_status` = ?' => 'no'
);

$task_data = $TaskHelper->taskAll($where);
//测试开关
$o_number = $this->Param['n'];
if($o_number) {
    $task_data = $OrderHelper->virtualTask($o_number, 'ordercar');
}

//REDIS 加锁 暂留
if(!empty($task_data)) foreach ($task_data as $k => $v){
    $order_info = $OrderHelper->orderInfo($v['task_releid']);
    $attach_arr = $AttachHelper->imageList($v['task_releid']);
    $data = array();
    /*if(!empty($order_info['buyer']['ct_attach'])){
        foreach($order_info['buyer']['ct_attach'] as $k => $v){
            $data[] = array(
                'source' => $v,
                'rename' => $order_info['buyer']['ct_name'].'_'.$OrderHelper->changeMeans($k).'.'.  end(explode('.', $v))
            );
        }
    }
    if(!empty($order_info['spouse']['ct_attach'])){
        foreach($order_info['spouse']['ct_attach'] as $k => $v){
            $data[] = array(
                'source' => $v,
                'rename' => $order_info['spouse']['ct_name'].'_'.$OrderHelper->changeMeans($k).'.'.  end(explode('.', $v))
            );
        }
    }
    if(!empty($order_info['guarantor'])){
        foreach($order_info['guarantor'] as $key => $val){
            $guarantor_att = unserialize($val['ct_attach']);
            foreach($guarantor_att as $k => $v){
                $data[] = array(
                    'source' => $v,
                    'rename' => $val['ct_name'].'_'.$OrderHelper->changeMeans($k).'.'.  end(explode('.', $v))
                );
            }
        }
    }*/
    if(!empty($order_info['attachment'])){
        $com_config = Config::GetCommon();
        $com_config = $com_config['attach_key'];
        foreach($order_info['attachment'] as $key => $val){
            if(isset($com_config[$key])){
                if(!empty($val)) foreach($val as $k => $vl){
                    $at_name = '';
                    if($com_config[$key]=='合同照' || $com_config[$key]=='流水类') {
                        $attachment_row = $AttachHelper->imageRow(array('`ai_path` = ?' => $vl));
                        if(empty($attachment_row)){
                            $attachment = $AttachHelper->imageRow2($attach_arr,$vl);
                        }
                        $name = isset($attachment['image_name'])? $attachment['image_name'] : $attachment_row['ai_title'];
                        $at_name = $attachment_row['ai_title']=='file'? '': $name ;
                    }
                    $data[] = array(
                        'source' => str_replace('http://crm.app.qiche100.com', '', $vl),
                        'rename' => $at_name==''? $com_config[$key].'_'.($k+1).'.'.  end(explode('.', $vl)):$at_name,
						'type' => $com_config[$key]=='家纺照'?'家访照':$com_config[$key]
                    );
                }
            }
        }
    }
    $result = $FtpUploadHelper->ofileSort($order_info['base']['o_number'], $data);
    if($o_number){
        echo print_r(array('r'=>$result, 'd'=>$data));
        exit;
    }
    else {
        if ($result) {
            $task_up = $TaskHelper->taskSave(array('task_status' => 'ok', 'task_dotime' => NOW_TIME, 'task_type' => $v['task_type']), $v['task_id']);
        }
        $TaskHelper->logSave(array(
            'tl_tid' => $v['task_id'],
            'tl_log' => serialize($result),
            'tl_dateline' => NOW_TIME
        ));
    }
}
echo '执行完毕'.date('Y-m-d H:i:s');